define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var tools = require("tools");
	var RemarkForm = require("./remarkForm");
    var instance = {
        initComponent: function (params,widget) {
        	var component = new Grid({
                parentNode: ".J-grid", 
                model : {
                    url : "api/travelcontractcharge/query",
                    params: function() {
                    	var subnav=widget.get("subnav");
                        return {
                        	"travelContract.signUpDate":subnav.getValue("dateRange").start,
     						"travelContract.signUpDateEnd":subnav.getValue("dateRange").end,
     						"status":subnav.getValue("status"),
     						fetchProperties: "*,travelContract.*,travelContract.member.personalInfo.*,travelContract.productRule.*," +
     								"travelContract.productRule.product.*,travelContract.productType.*," +
     								"travelContract.productBatch.*,travelContract.status.*,status.*" 
                        };
                    },
                    columns : [{
        				name:"travelContract.contactNo",
        				label:"合同号",
        			},{
						name:"travelContract.member.personalInfo.name",
						label:"会员"
					},{
        				name:"travelContract.member.personalInfo.mobilePhone",
        				label:"联系电话"        				
					},{						
						name:"travelContract.productRule.product.name",
        				label:"产品",
					},{
        				name:"travelContract.productType.name",
        				label:"产品类型",
        			},{
						name:"travelContract.number",
						label:"购买份数"
					},{
        				name:"travelContract.price",
        				label:"金额（元）",
        				format: function(value, rowData) {
			                return value.toFixed(2);
			            }
					},{
						name:"travelContract.signUpDate",
						label:"签约日期",
						format:"date"
					},{
        				name:"travelContract.status.value",
        				label:"签约状态",
        			},{
						name:"status.value",
						label:"收费状态"
					},{
						name:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"charge",
							text: "收费",
							show:function(value,data){
								if (data.status.key == "NoPay") {
									return true;
			                    } else {
			                        return false;
			                    }
							},
							handler:function(index,data,rowEle){
								var remarkForm = RemarkForm.initComponent();
								Dialog.showComponent(remarkForm,{
									defaultButton:false,
									title: "签约收费",
									setStyle: function(){
										$(".el-dialog-modal .modal").css({
											"top": "10%",
											"width":"45%",
											"left": "25%",
										});
									},
									buttons :[{
										id:"save",
										text:"保存",
	 									className:"btn-primary",
										handler:function(){
											if(remarkForm.valid()){
												var FormData = remarkForm.getData();
												var saveData = {};
												saveData.pkTravelSignUpCharge = FormData.pkTravelSignUpCharge;
												saveData.version = FormData.version;
												saveData.description = FormData.description;
												saveData.paymentMethod = FormData.paymentMethod;
												saveData.travelContract = {
														pkTravelContract:FormData.pkTravelContract,
														status: "Valid",
														version:FormData.versionTravelContract
												};
												saveData.status = "Pay";
												
												Dialog.alert({
													content : "保存中，请稍候。",
													defaultButton : false
												});
												aw.saveOrUpdate("api/travelcontractcharge/savecontract",aw.customParam(saveData),function(saveData){
													Dialog.close();
													Dialog.mask(false);
								          			widget.get("grid").refresh();
								       			});
											};
										}
									},{
										id:"close",
										text:"取消",
										handler:function(){
											Dialog.close();
										}
									}]
								});
								remarkForm.setValue("contactNo",data.travelContract.contactNo);
								remarkForm.setValue("priceSum",parseFloat(data.travelContract.price).toFixed(2));
								remarkForm.setValue("pkTravelSignUpCharge",data.pkTravelSignUpCharge);
								remarkForm.setValue("version",data.version);
								remarkForm.setValue("pkTravelContract",data.travelContract.pkTravelContract);
								remarkForm.setValue("versionTravelContract",data.travelContract.version);
							}
						}]
					}]
                }
            });
            widget.set("grid",component);
        }
    };
    module.exports = instance;
});